package com.hpin.mlxteacher.child;

import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.HistoryAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.SingleSignResule;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
import com.hpin.mlxteacher.widget.MyDatePicker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 个人历史考勤记录页面
 * Created by Aaron on 2015/7/28.
 */
public class SingleHistoryActivity extends BaseActivity {

    private String nickName;
    private String stuId;
    private String choosedMonth;
    private TextView tv_date;
    private TextView tv_am;
    private TextView tv_noon;
    private TextView tv_pm;
    private List<SingleSignResule.SignDay> signDayList;
    private HistoryAdapter adapter;

    private String lastMonth = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_single_history);
        nickName = getIntent().getStringExtra("nickName");
        stuId = getIntent().getStringExtra("stuId");

        signDayList = new ArrayList<SingleSignResule.SignDay>();

        initWidget();

        Calendar c = Calendar.getInstance();

        String month = (c.get(Calendar.MONTH) + 1) < 10 ? "0" + (c.get(Calendar.MONTH) + 1) : (c.get(Calendar.MONTH) + 1) + "";
        choosedMonth = c.get(Calendar.YEAR) + "-" + month;

        tv_date.setText(c.get(Calendar.YEAR) + "-" + month);

        getSignHistory();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(nickName +"历史");
        tv_right.setText(getResources().getString(R.string.choose_time));

        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_am = (TextView) findViewById(R.id.tv_am);
        tv_noon = (TextView) findViewById(R.id.tv_noon);
        tv_pm = (TextView) findViewById(R.id.tv_pm);

        ListView lv_details = (ListView) findViewById(R.id.lv_details);

        adapter = new HistoryAdapter(this, signDayList);
        lv_details.setAdapter(adapter);

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

    }

    //获取签到历史记录
    private void getSignHistory() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("id", stuId);
        j.put("month", choosedMonth);

        HttpUtil.setPostRequest("查询中...", this, Constant.WEB_ROOT + Constant.CHECKFORHISTORY, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            SingleSignResule signResule = JSONObject.parseObject(str, SingleSignResule.class);

                            if (signResule.code == Constant.SUCCESS) {

                                tv_am.setText("上午到" + signResule.morning + "次");
                                tv_noon.setText("中午到" + signResule.afternoon + "次");
                                tv_pm.setText("下午到" + signResule.night + "次");

                                adapter.setSignDayList(signResule.data);
                                adapter.notifyDataSetChanged();

                            }else {
                                showToast(Constant.showErrorMsg(signResule.code, SingleHistoryActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("查询记录失败");
                        }

                    }
                }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }


    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right://选择日期

                Calendar c = Calendar.getInstance();

                new MyDatePicker(SingleHistoryActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";

                        tv_date.setText(year + "-" + month);

                        choosedMonth = year + "-" + month;

                        if (choosedMonth.equals(lastMonth)) {
                            return;
                        } else {
                            lastMonth = choosedMonth;
                        }

                        getSignHistory();

                    }


                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();
                break;

        }
    }

//    public void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
//    }
//
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPause(this);
//    }
}
